package com.thon.service.user.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectSupportDao;
import com.thon.dao.user.FundDetailDao;
import com.thon.dao.user.UserDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectSupport;
import com.thon.entity.user.FinanceView;
import com.thon.entity.user.FundDetail;
import com.thon.entity.user.User;
import com.thon.service.user.FundDetailService;

@Service
@Transactional
public class FundDetailServiceImpl implements FundDetailService {

	@Autowired
	private FundDetailDao fundDetailDao;

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ProjectSupportDao projectSupportDao;
	
	@Override
	public Page<FundDetail> findFundDetailsByProject(Integer projectId,
			int pageNo, int pageSize) {

		Page<FundDetail> page = new Page<FundDetail>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		return fundDetailDao.finDetailsByProject(page, projectId);
	}

	@Override
	public Page<FundDetail> findFundDetailsByUser(Integer userId,Integer status, int pageNo,
			int pageSize) {
		Page<FundDetail> page = new Page<FundDetail>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return fundDetailDao.finDetailsByUser(page,status,userId);
	}

	@Override
	public void addFundDetail(FundDetail fundDetail) {

		fundDetailDao.save(fundDetail);
	}

	@Override
	public Page<FundDetail> findFundDetailsByUser(Integer userId, int pageNo,
			int pageSize, Long days) {
		Page<FundDetail> page = new Page<FundDetail>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		Date now = new Date();
		Date start = new Date(now.getTime() - days * 24 * 60 * 60 * 1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return fundDetailDao.finDetailsByUser(page, userId, sdf.format(start));
	}

	@Override
	public FundDetail getFundDetail(String tradeNo) {
		return fundDetailDao.getFundDetail(tradeNo);
	}
	
	@Override
	public FundDetail getFundDetail(Integer userId) {
		return fundDetailDao.getFundDetail(userId);
	}

	@Override
	public void updateFundDetail(FundDetail fundDetail) {
		fundDetailDao.update(fundDetail);
	}
	
	@Override
	public Page<FundDetail> findFundDetails(int pageNo, int pageSize,int actionType) {
		Page<FundDetail> page=new Page<FundDetail>(pageSize);
		page.setPageNo(pageNo);
		return fundDetailDao.findDetails(page,actionType);
	}

	@Override
	public Page<FinanceView> findFinace(int p, int s) {
		Page<FinanceView> financePage=new  Page<FinanceView>();
		financePage.setPageNo(p);
		financePage.setPageSize(s);
		Page<ProjectSupport> supportPage=new Page<ProjectSupport>();
		supportPage.setPageNo(p);
		supportPage.setPageSize(s);
		List<FinanceView> fvs=new ArrayList<FinanceView>();
			
		Page<ProjectSupport> projectSupports=projectSupportDao.findProjectSupport(supportPage);
		Integer sum=0;
		if(projectSupports.getResult()!=null)
			for(ProjectSupport ps:projectSupports.getResult()){
				FinanceView fv=new FinanceView();
				if(!ps.getUser().getId().equals(ps.getId())){								
					sum=projectSupportDao.sumFundByUser(ps.getProject().getId(), ps.getUser().getId()).intValue();
					fv.setProject(ps.getProject());
					fv.setDate(ps.getCreatedDate());						
					fv.setUser(ps.getUser());
					Integer fundSum=fundDetailDao.getSumDetailByUser(ps.getUser().getId());
					fv.setUserSumFund(fundSum);
					fv.setSupport(sum);
					fv.setId(ps.getId());
				}
				else{
					fv.setProject(null);
					fv.setDate(null);
					fv.setUser(userDao.getUser(ps.getId()));
					Integer fundSum=fundDetailDao.getSumDetailByUser(ps.getUser().getId());
					fv.setUserSumFund(fundSum);
					fv.setSupport(0);
					fv.setId(ps.getId());
					
				}
				fvs.add(fv);
			}
			financePage.setPageNo(projectSupports.getPageNo());
			financePage.setAutoCount(projectSupports.getAutoCount());
			financePage.setLength(projectSupports.getLength());
			financePage.setTotalCount(projectSupports.getTotalCount());
			financePage.setResult(fvs);
		
		return financePage;
	}

	@Override
	public void saveFundDetail(FundDetail fundDetail) {
		
		fundDetailDao.save(fundDetail);
	}

	

}
